#version 450 core

layout(location = 0) out vec4 FragColor;

in vec3 FragPos;
in vec2 TexCoord;
in vec3 Normal;
in vec3 Tangent;
in vec3 Bitangent;

layout(std430, binding = 0) buffer KernelCoef
{
	vec4 data[];
} kernel_coef;

uniform int mode;
uniform int area;
uniform int tex_h;
uniform int tex_w;
uniform int kernel_h;
uniform int kernel_w;
uniform int coef_n;
uniform vec3 view_pos;
uniform float A;
// layout(rgba32f, binding = 0) uniform image2D radiance_map;
// layout(rgba32f, binding = 1) uniform image2D radiance_map_after_haar;
// layout(rgba32f, binding = 2) uniform image2D radiance_map_after_sss;
layout(binding = 0) uniform sampler2D diffuse;
layout(binding = 1) uniform sampler2D normal;
layout(binding = 31) uniform sampler2D radiance_map_after_sss;

// vec3 colorAt(vec2 coord);
// vec3 map(vec3 value, vec3 inMin, vec3 inMax, vec3 outMin, vec3 outMax);

void main()
{
	int light_n = 2;
	vec3 light_dir[2] = {normalize(vec3(1.0, 1.0, 1.0)), normalize(vec3(-1.0, -1.0, -1.0))};
	vec3 light_color[2] = {vec3(1, 1, 1), vec3(0.8, 0.8, 0.8)};
	vec3 lighting = vec3(0, 0, 0);
	for (int i = 0; i < light_n; i++)
	{
		vec3 normal_ts = texture(normal, TexCoord).xyz * 2 - 1;
		vec3 normal = normalize(normal_ts.x * Tangent + normal_ts.y * Bitangent + normal_ts.z * Normal);
		lighting += max(dot(normal, light_dir[i]), 0.0) * light_color[i] * texture(diffuse, TexCoord).rgb;
	}
	if (mode == 0)
	{
		vec3 sss_color = A * texture(diffuse, TexCoord).rgb * texture(radiance_map_after_sss, vec2(TexCoord.x, 1-TexCoord.y)).rgb;
		lighting = sss_color;
	}
	else if (mode == 1)
	{
		vec3 sss_color = A * texture(diffuse, TexCoord).rgb * texture(radiance_map_after_sss, vec2(TexCoord.x, 1-TexCoord.y)).rgb;
		// lighting += sss_color;
		lighting = mix(lighting, sss_color, 0.5);
	}
	// lighting = vec3(1, 1, 1);

	FragColor = vec4(lighting, 1.0);
	// FragColor = vec4(texture(normal, TexCoord).xyz, 1.0);
}
